package com.wohuijituan.whcsmerchant.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.wohuijituan.whcsmerchant.context.MyApplication;
import com.wohuijituan.whcsmerchant.myview.MyProgressDialog;
import com.wohuijituan.whcsmerchant.utils.MyOkHttpUtils;
import com.wohuijituan.whcsmerchant.utils.MyRequestParams;

/**
 * 说明：
 * 作者：朱世元
 * 时间： 2016/8/14 15:09
 * 版本：V1.0
 * 修改历史：
 */
public abstract class MyBaseFragment extends Fragment{
    Context ctx;
    Activity activity;
    MyApplication myApplication;
    MyOkHttpUtils myOkHttpUtils;
    MyRequestParams params;
    private View cacheView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ctx = getContext();
        activity = getActivity();
        myApplication = (MyApplication) activity.getApplication();
        myOkHttpUtils = new MyOkHttpUtils();
        params = new MyRequestParams();
        if (cacheView==null){
            cacheView = createMyView(inflater,container,savedInstanceState);
        }
        return cacheView;
    }
    protected abstract View createMyView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState);
    /**
     * 弹出toast
     *
     * @param ctx context
     * @param str 内容
     */
    public static void showToast(Context ctx, String str) {
        Toast.makeText(ctx, str, Toast.LENGTH_SHORT).show();
    }
    public MyProgressDialog myProgressDialog;

    /**
     * 显示进度框
     *
     * @param ctx Context
     */
    public void showDialog(Context ctx) {
        if (myProgressDialog == null || !myProgressDialog.isShowing()) {
            myProgressDialog = MyProgressDialog.create(ctx).show();
        }
    }

    /**
     * 关闭弹窗
     */
    public void dismissDialog() {
        myProgressDialog.dismiss();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (null != cacheView) {
            ((ViewGroup) cacheView.getParent()).removeView(cacheView);
        }
    }
}
